﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Icst.Leaflike.Logging;

namespace Icst.Leaflike.Data
{
    public class SysSetting : DataContext<SysSetting>
    {
        protected static readonly string CACHE_ALL = "/SysSetting/All";

        public string this[string name]
        {
            get { return ListAll().Where(p => p.name == name).Select(p => p.value).SingleOrDefault(); }
        }
        public sys_setting[] ListAll()
        {
            try
            {
                sys_setting[] results =
                    CachePolicy.GetIf<sys_setting[]>(CACHE_ALL, () => { return Context.sys_settings.ToArray(); });
                return results;
            }
            catch (Exception e)
            {
                Logger.Error("FAILED TO LOAD SYSTEM SETTINGS. " + e.Message);
                throw;
            }
        }
        public override void Refresh()
        {
            base.Refresh();

            try { CachePolicy.Delete(CACHE_ALL); }
            catch { }
        }
    }
}
